﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PVFSLEAP
{
	class C_EVNT
	{
		F_SETT	ST = new F_SETT();
		C_NODE	ND = new C_NODE();
		C_PVFS	PV = new C_PVFS();
		C_COMM	CM = new C_COMM();

		Random C = new Random( ( int )DateTime.Now.Millisecond % 999 );
		Random K = new Random( ( int )DateTime.Now.Millisecond % 999 );

		public int chooseCH()
		{
			//System.Threading.Thread.Sleep( 1 );
			int c = C.Next( 1, 100 );
			return c;
		}

		public int getKey()
		{
			System.Threading.Thread.Sleep( 1 );
			int k = K.Next( 0, ST.OCHLD - 1 );
			return k;
		}

		public void attackFRIA( C_NODE[] SN )
		{
			int chnd = -1;
			int ndid = -1;
			int mvid = -1;
			int tvfv = 0;

			int vote = 0;

			chnd = chooseCH();
			//while ( true )
			//{
			//    chnd = chooseCH();
			//    if ( SN[chnd].CPNF == true )
			//        break;
			//}
			
			PV.generateReport( chnd, SN );
			ndid = chnd;

			for ( int i=0; i < C_PVFS.vote.Count; i++ )
			{
				if ( C_PVFS.vote.GetByIndex(i).ToString() == "T" )
					vote++;
			}

			if ( ( vote != 0 ) && ( vote < ST.OTVFV ) )
				SN[chnd].OFVI++;
			else if ( vote == ST.OTVFV )
				SN[chnd].OFRI++;

			//int numk = PV.generateKeys();
			//for ( int i = 0; i < numk; i++ )
			//{
			//SN[chnd].ENGY = CM.consumeTransmitting( SN[chnd].ENGY, 8 * numk );
			//SN[chnd].ENGY = CM.consumeReceiving( SN[chnd].ENGY, 8 * numk );			
			//}

			while ( true )
			{
				mvid = SN[ndid].PRID;
				SN[ndid].ENGY = CM.consumeReceiving( SN[ndid].ENGY, 24 );
				
				if ( ST.OPVFS == C_COMM.TRUE )
				{
					if ( PV.checkVerifiedNode( SN[mvid].HPCT, SN[chnd].HPCT ) == true )
					{
						int numk = PV.generateKeys();

						for ( int i = 0; i < SN[chnd].HPCT - SN[mvid].HPCT; i++ )
						{
							// Key Bytes
							SN[mvid].ENGY = CM.consumeTransmitting( SN[mvid].ENGY, numk * 4 );
							SN[mvid].ENGY = CM.consumeReceiving( SN[mvid].ENGY, numk * 4 );
						}

						for ( int i = 0; i < numk; i++ )
						{
							int keys = getKey();

							if ( C_PVFS.vote.Contains( keys ) == true )
							{
								if ( C_PVFS.vote[ keys ].ToString() == "T" )
								{
									if ( vote == 1 )
										SN[mvid].DFVI++;

									tvfv++;
									C_PVFS.vote[keys] = "F";
									break;
								}
							}
						}
					}
				}
				else if ( ST.OPPMT == C_COMM.TRUE )
				{
					if ( ndid == chnd )
					{
						for ( int i=0; i < C_PVFS.vote.Count; i++ )
						{
							if ( C_PVFS.vote.GetByIndex(i).ToString() == "T" )
							{
								if ( vote == 1 )
									SN[mvid].DFVI++;

								C_PVFS.vote[C_PVFS.vote.GetKey(i)] = "F";
								tvfv++;
							}
						}
					}
				}
				
				if ( tvfv >= ST.OTVFV )
				{
					SN[mvid].DFRI++;
					break;
				}

				if ( SN[mvid].NDID == 0 )
					break;

				SN[mvid].ENGY = CM.consumeTransmitting( SN[mvid].ENGY, 24 );
				ndid = mvid;
			}
		}

		public void attackWormhole( C_NODE[] SN )
		{
			if ( ( ST.OLEAP == C_COMM.TRUE ) || ( ST.PPMT == C_COMM.TRUE ) )
			{
				for ( int i = 1; i < ST.OTTND; i++ )
				{
					if ( SN[i].WORM == true )
					{
						//for ( int c = 0; c < ST.OCHLD; c++ )
						//{
							double engy = CM.consumeReceiving( SN[i].ENGY, 1 );
							//SN[i].ENGY = engy * ST.OCHLD;
							SN[i].ENGY = engy*3;
							SN[i].OWRM++;
							SN[i].DWRM++;
							engy = CM.consumeTransmitting( SN[i].ENGY, 1 );
							//SN[i].ENGY = engy * ST.OCHLD;
							SN[i].ENGY = engy*3;
						//}
					}
				}
			}
		}
	}
}
